
var admin={
	// 封装ajax请求，返回数据类型为json
    req: function (url, data,dataType, success, method) {
        if ('put' == method.toLowerCase()) {
            method = 'POST';
            data._method = 'PUT';
        } else if ('delete' == method.toLowerCase()) {
            method = 'POST';
            data._method = 'DELETE';
        }

        if(!dataType){
            dataType="json"
        }

        admin.ajax({
            url: url,
            data: data,
            type: method,
            dataType: dataType,
            success: success
            
        });
    },
    
    // 封装ajax请求
    ajax: function (param) {
        var successCallback = param.success;
        param.success = function (result, status, xhr) {
            // 判断登录过期和没有权限
            var jsonRs;
            if ('json' == param.dataType.toLowerCase()) {
                jsonRs = result;
            } else if ('html' == param.dataType.toLowerCase() || 'text' == param.dataType.toLowerCase()) {
                jsonRs = admin.parseJSON(result);
            }
            if (jsonRs) {
                if (jsonRs.code == 401) {
                    config.removeToken();
                    layer.msg('登录过期', {icon: 2, time: 1500}, function () {
                        location.replace('/login.html');
                    }, 1000);
                    return;
                } else if (jsonRs.code == 403) {
                    layer.msg('没有权限', {icon: 2});
                    return;
                }
            }
            successCallback(result, status, xhr);
        };
        param.error = function (xhr) {
            param.success({code: xhr.status, msg: xhr.statusText});
        };
        $.ajax(param);
    },
    // 判断是否为json
    parseJSON: function (str) {
        if (typeof str == 'string') {
            try {
                var obj = JSON.parse(str);
                if (typeof obj == 'object' && obj) {
                    return obj;
                }
            } catch (e) {
            }
        }
    },
    msgSucess:function(obj,title,message){
    	obj.$message({
            title: title,
            message: message,
            type:'success'
          });
    },
    msgError:function(obj,title,message){
    	obj.$message({
            title: title,
            message: message,
            type:'warning'
          });
    }
}